package com.ruoyi.keep.service.impl;

import java.util.List;

import com.ruoyi.keep.domain.CommonUsers;
import com.ruoyi.keep.domain.Informisnew;
import com.ruoyi.keep.mapper.CommonUsersMapper;
import com.ruoyi.keep.mapper.InformisnewMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.keep.mapper.InformMapper;
import com.ruoyi.keep.domain.Inform;
import com.ruoyi.keep.service.IInformService;

/**
 * 通知Service业务层处理
 * 
 * @author zqd
 * @date 2024-08-14
 */
@Service
public class InformServiceImpl implements IInformService 
{
    @Autowired
    private InformMapper informMapper;

    @Autowired
    private InformisnewMapper informisnewMapper;

    @Autowired
    private CommonUsersMapper commonUsersMapper;

    /**
     * 查询通知
     * 
     * @param id 通知主键
     * @return 通知
     */
    @Override
    public Inform selectInformById(Long id)
    {
        return informMapper.selectInformById(id);
    }

    /**
     * 查询通知列表
     * 
     * @param inform 通知
     * @return 通知
     */
    @Override
    public List<Inform> selectInformList(Inform inform)
    {
        return informMapper.selectInformList(inform);
    }

    /**
     * 新增通知
     * 
     * @param inform 通知
     * @return 结果
     */
    @Override
    public int insertInform(Inform inform) {
        int inform1 = informMapper.insertInform(inform);
//查询所有用户
        List<CommonUsers> commonUsers = commonUsersMapper.selectCommonUsersList(null);
//遍历所有用户，并把id插入是否已读表
        for (CommonUsers user : commonUsers) {
            Informisnew informisnew = new Informisnew();
//获取新增公告id插入已读状态表
            informisnew.setInformid(inform.getId());
            informisnew.setUserid(Long.valueOf(user.getId()));
            informisnew.setIsNew(0L);// 标记为未读
            informisnewMapper.insertInformisnew(informisnew);
        }






////       创建是否已读实体对象
//        Informisnew informisnew = new Informisnew();
////       获取插入公告时的公告id插入用户是否已读的实体
//        informisnew.setInformid(inform.getId());
//        informisnewMapper.insertInformisnew(informisnew);
        return inform1;
    }

    /**
     * 修改通知
     * 
     * @param inform 通知
     * @return 结果
     */
    @Override
    public int updateInform(Inform inform)
    {
        return informMapper.updateInform(inform);
    }

    /**
     * 批量删除通知
     * 
     * @param ids 需要删除的通知主键
     * @return 结果
     */
    @Override
    public int deleteInformByIds(Long[] ids)
    {
        return informMapper.deleteInformByIds(ids);
    }

    /**
     * 删除通知信息
     * 
     * @param id 通知主键
     * @return 结果
     */
    @Override
    public int deleteInformById(Long id)
    {
        return informMapper.deleteInformById(id);
    }

    @Override
    public List<Inform> selectInformByUserid(Long id) {
        List<Inform> list = informMapper.selectInformByUserid(id);
        return list;
    }


}
